package com.gzx.plugin.tjzy.modular.liaison.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.Cell;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.gzx.plugin.tjzy.modular.liaison.entity.vo.GzxDebtorLiaisonImportVo;
import org.apache.commons.lang3.StringUtils;
import vip.xiaonuo.common.listener.CommonExcelListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zjt
 * @description
 * @date 2025/2/5 14:33
 */
public class LiaisonExcelListener extends CommonExcelListener<GzxDebtorLiaisonImportVo> {

    private final List<GzxDebtorLiaisonImportVo> debtorLiaisonImportVoList = new ArrayList<>();

    private Map<Integer, String> headMap;

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        this.headMap = headMap;
    }

    @Override
    public void customAnalyse(GzxDebtorLiaisonImportVo debtorLiaisonImportVo, AnalysisContext analysisContext) {
        List<String> phoneNumberList = new ArrayList<>();
        Map<Integer, Cell> cellMap = analysisContext.readRowHolder().getCellMap();
        for (Map.Entry<Integer, Cell> entry : cellMap.entrySet()) {
            if ("联系人电话".equals(headMap.get(entry.getKey()))) {
                ReadCellData readCellData = (ReadCellData) cellMap.get(entry.getKey());
                if (StringUtils.isNotEmpty(readCellData.getStringValue())) {
                    phoneNumberList.add(String.valueOf(readCellData.getStringValue()));
                }
                if (readCellData.getNumberValue() != null) {
                    phoneNumberList.add(String.valueOf(readCellData.getNumberValue()));
                }
            }
        }
        debtorLiaisonImportVo.setLiaisonPhoneList(phoneNumberList);
        debtorLiaisonImportVoList.add(debtorLiaisonImportVo);
    }

    public List<GzxDebtorLiaisonImportVo> getExcelResult() {
        return debtorLiaisonImportVoList;
    }
}
